// pages/myShare/myShare.js
import * as API from '../../../api/myShareApi';
import * as util from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 10,
    total: 0,
    sectionNum: 0,
    test: 1,
    networkError: false,
    customId: '',
    placeholder: '搜索销售订单号/商品名称/编码搜索',
    selectOrder: [],
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('bobobon')
    // wx.setStorageSync('customId', options.customId)
    if (options && options.customId) {
      this.setData({
        customId: options.customId
      })
    }
    this.getOrderList();
  },
  getOrderList: function (searchWords) {
    let postBody = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
      customId: this.data.customId,
      orderByWhat: 0,
      sort: 'DESC'
    }
    if (searchWords) {
      postBody.searchWords = searchWords;
    }
    API.orderList(postBody).then(res => {
      this.showColor(res, searchWords)
    }, err => {
      util.toast(err)
    })
  },
  onHandleSearch: function (e) {
    util.preventMoreClick(this, () => {
      let searchWords = e.detail;
      if (!searchWords) {
        util.toast('请输入搜索关键字')
        return;
      }
      this.setData({
        selectOrder: []
      })
      wx.removeStorageSync('selectOrder');
      this.getOrderList(searchWords);
    })
  },
  clearSearch: function (e) {
    this.setData({
      selectOrder: [],
      pageNo: 1,
      total: 0
    })
    wx.removeStorageSync('selectOrder');
    this.switchCheck();
    this.getOrderList();
  },

  switchCheck: function (e) {
    let id;
    let data;
    if (e) {
      id = e.currentTarget.dataset.id || '';
      data = {
        orderList: this.data.orderList,
        selectOrder: this.data.selectOrder
      }
    } else {
      id = '';
      data = {
        orderList: [],
        selectOrder: []
      }
    }
    data.orderList.forEach((item, index) => {
      if (item.id == id) {
        // item.showBtn=true
        if (item.showBtn == true) {
          item.showBtn = false
          this.arrayRemove(data.selectOrder, id)
        } else {
          if (this.data.selectOrder.length >= 100) {
            wx.showToast({
              title: '单次最多选择100条',
              icon: 'none'
            })
            return;
          }
          item.showBtn = true
          data.selectOrder.push(id)
        }
      }
      if (!e) {
        item.showBtn = false
      }
    });
    this.setData(data)
  },
  toDetail: function () {
    wx.setStorageSync('selectOrder', this.data.selectOrder)
    wx.navigateTo({
      url: '/pages/myShare/detail/detail'
    })
  },
  arrayRemove: function (arr, elem) {
    let index = arr.indexOf(elem);
    if (index > -1) {
      arr.splice(index, 1)
    }
  },
  showColor: function (res, searchWords) {
    let selectOrder = wx.getStorageSync('selectOrder')
    let data = {
      orderList: res.orderList,
      total: res.totalCount
    }
    if (selectOrder && selectOrder.length > 0) {
      this.setData({
        selectOrder: selectOrder
      })
      data.orderList.map(item => {
        item.showBtn = false
        for (let i = 0; i < selectOrder.length; i++) {
          if (item.id == selectOrder[i]) {
            item.showBtn = true
          }
        }
      })
    } else {
      data.orderList.map(item => {
        item.showBtn = false
      })
    }
    if (!searchWords) {
      data.orderList = this.data.orderList.concat(data.orderList);
    }
    if (data.orderList.length > 0) {
      this.setData({
        networkError: false
      })
    } else {
      this.setData({
        networkError: true
      })
    }
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 增量加载
    var hasMore = this.data.orderList.length < this.data.total;
    console.log(hasMore)
    if (hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.getOrderList();
    }
  }
})